﻿using PageAdmin.Utils.ORM;
using System;
using System.Collections.Generic;
using System.Web;
namespace PageAdmin.Utils
{
    public class WebLogHelper
    {
        /// <summary>
        /// 往主系统日志表中插入记录
        /// </summary>
        /// <param name="webLogTypeEnum"></param>
        /// <param name="message"></param>
        /// <param name="user"></param>
        /// <param name="url"></param>
        /// <param name="ip"></param>
        public static void Add(DataBaseContext dbContext, WebLogTypeEnum webLogTypeEnum, string message, CurrentUser user = null, string url = "", string ip = "")
        {
            HttpContext current = HttpContext.Current;
            DbKeyValueList dic = new DbKeyValueList();
            dic.Add("logType", webLogTypeEnum);
            if (user != null) //表示有用户在操作，而不是系统自带的操作
            {
                dic.Add("uid", user.Uid);
                dic.Add("username", user.UserName, 50);
            }
            dic.Add("url", url, 200);
            dic.Add("ip", ip, 50);
            dic.Add("message", message, 500);
            dic.Add("thedate", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            dbContext.Insert("pa_web_log", dic);
        }

        /// <summary>
        /// 往主系统日志表中插入记录
        /// </summary>
        /// <param name="webLogTypeEnum"></param>
        /// <param name="message"></param>
        /// <param name="user"></param>
        /// <param name="url"></param>
        /// <param name="ip"></param>
        public static void Add(WebLogTypeEnum webLogTypeEnum, string message, string userToken="", string url = "", string ip = "")
        {
            WebApiHelper webApiHelper = new WebApiHelper();
            Dictionary<string, string> dicHeader = new Dictionary<string, string>();
            dicHeader.Add("usertoken", userToken);
            Dictionary <string, string> dic = new Dictionary<string, string>();
            dic.Add("Method", "WebLog.Add");
            dic.Add("webLogType", ((int)webLogTypeEnum).ToString());
            dic.Add("Message", message);
            dic.Add("url", url);
            dic.Add("ip", ip);
            webApiHelper.HeaderDictionary = dicHeader;
            webApiHelper.Post(dic);
        }
    }


    //日志类型
    public enum WebLogTypeEnum
    {
        /// <summary>
        ///  应用级日志
        /// </summary>
        ApplicationLog = 0,
        /// <summary>
        /// 错误日志
        /// </summary>
        ErrorLog = 1,
        /// <summary>
        /// 主动任务相关的日志
        /// </summary>
        AutotaskLog = 2,

        /// <summary>
        /// 管理员日志，记得在后台的一些常用操作日志
        /// </summary>
        AdminerLog = 3,
        /// <summary>
        /// 会员日志，记得前台用户的一些常用操作日志
        /// </summary>
        MemberLog = 4,
        /// <summary>
        /// 管理员登录日志
        /// </summary>
        AdminLoginLog = 5,

        /// <summary>
        /// 会员登录日志
        /// </summary>
        MemberLoginLog = 6,

        /// <summary>
        /// 升级相关的日志
        /// </summary>
        UpgradeLog = 11,
        /// <summary>
        /// 其他日志
        /// </summary>
        ElseLog = 100
    }
}
